package com.hdoit.ares.web.cust.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.ares.logical.cust.model.AdviceFeedback;
import com.hdoit.ares.logical.cust.service.IAdviceFeedbackService;
import com.hdoit.ares.logical.viewmodel.cust.AdviceFeedbackSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

@Controller
@RequestMapping(value="/admin/cust/adviceFeedback/")
public class AdviceFeedbackController extends BaseController{

	@Override
	public String getRoutePath() {
		return "areas/admin/cust/adviceFeedback/";
	}
	
	private IAdviceFeedbackService adviceFeedbackService;
	
	public IAdviceFeedbackService getAdviceFeedbackService() {
		return adviceFeedbackService;
	}

	@Resource
	public void setAdviceFeedbackService(
			IAdviceFeedbackService adviceFeedbackService) {
		this.adviceFeedbackService = adviceFeedbackService;
	}
	
	private final String indexViewName ="index";
	private final String outputViewName ="output";
	private final String detailedViewName ="adviceFeedbackDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";
	
	
	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String index(Model model,PagerModel pagerModel,AdviceFeedbackSearch adviceFeedbackSearch,boolean ajaxPost){
		pagerModel.setTotalAmount(adviceFeedbackService.queryCount(adviceFeedbackSearch));
		
		List<AdviceFeedback>adviceFeedbacks = adviceFeedbackService.queryList(adviceFeedbackSearch, pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY,pagerModel);
		model.addAttribute("adviceFeedbacks", adviceFeedbacks);
		model.addAttribute("adviceFeedbackSearch", adviceFeedbackSearch);
		
		if(ajaxPost){
			return view(outputViewName);
		}
		
		return view(indexViewName);
	}
	
	@RequestMapping(value = detailedViewName ,method = {RequestMethod.GET})
	public String adviceFeedbackDetailed(Model model,String key){
		AdviceFeedback adviceFeedback = new AdviceFeedback();
		if(!StringUtils.isEmpty(key)){
			adviceFeedback = adviceFeedbackService.queryModel(key);
		}
		model.addAttribute("adviceFeedback",adviceFeedback);
		return view(detailedViewName);
	}
	
	@RequestMapping(value=saveViewName,method={ RequestMethod.GET,RequestMethod.POST })
	public String save(Model model,AdviceFeedback adviceFeedback){
		adviceFeedback.setModifyDate(DateUtils.getCurrent());
		adviceFeedback.setName(getLoginModel().getLoginName());
		adviceFeedback.setModelStatus(ModelStatus.update);
		ActionResult actionResult;
		if(adviceFeedbackService.applyChange(adviceFeedback)){
			actionResult = new ActionResult();
		}else{
			actionResult = new ErrorResult();
		}
		model.addAttribute("adviceFeedback",adviceFeedback);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		return view(detailedViewName);
	}
	
	 @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
	    public @ResponseBody String delete(String key){
	        if(adviceFeedbackService.deleteFakeByKey(key)){
	            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
	        }
	        return new JsonActionResult(ManageContainer.ERROR).toJson();
	    }
}
